#include <stdlib.h>
#include <stdio.h>
#include <cogent.h>

#include "trenddb.h"

IP_Task *myself;
IP_Task *hist;

//----------------------------------------------------------------------------
//
//----------------------------------------------------------------------------
int trenddb_init_ip (char *trenddb_name, IP_Task **myself, IP_Task **hist)
{
	char namebuf [255];
	
	sprintf (namebuf, "ph_view_%d", getpid ());
	if (!(*myself = IP_TaskCreateMe (IP_GetChannelID(), namebuf, NULL, NULL, 0))) {
	    fprintf (stderr, "Can't initialize Cascade IPC subsystem for name \"%s\"\n", namebuf);
	    return (-1);
	}	
	if (!(*hist = IP_NserveLookupName (trenddb_name))) {
		fprintf (stderr, "Can't lookup name in Cascade IPC: \"%s\"\n", trenddb_name);
		return (-1);
	}
	
	return 0;
}

//----------------------------------------------------------------------------
//
//----------------------------------------------------------------------------
int trenddb_destroy_ip (IP_Task **myself, IP_Task **hist)
{
	IP_TaskDestroy (*myself);
	*myself = NULL;
	
	IP_TaskDestroy (*hist);
	*hist = NULL;

	return 0;
}

//----------------------------------------------------------------------------
//
//----------------------------------------------------------------------------
int trenddb_flush (IP_Task *myself, IP_Task *hist)
{
	IP_Msg			*hmsg;
	char			data [512];
	int				len;
	
	hmsg = IP_MsgCreate (NULL, IP_MsgDefaultSize(), 0);
		
	len = sprintf (data, "flush") + 1;
	IP_MsgCascade (hmsg, data, len, IP_NONE, ST_OK);
	IP_TaskSendSync (myself, hist, hmsg, hmsg);
	if (!strcmp ((char*) hmsg->msg->data, "ERROR")) {
		printf ("flush reply: %s\n", (char*) hmsg->msg->data);
		return -1;
	}	
	
	IP_MsgDestroy (hmsg);

	return 0;
}

//----------------------------------------------------------------------------
//
//----------------------------------------------------------------------------
int trenddb_start_new_file (IP_Task *myself, IP_Task *hist)
{
	IP_Msg			*hmsg;
	char			data [512];
	int				len;
	
	hmsg = IP_MsgCreate (NULL, IP_MsgDefaultSize(), 0);
	
	len = sprintf (data, "start_new_file") + 1;
	IP_MsgCascade (hmsg, data, len, IP_NONE, ST_OK);
	IP_TaskSendSync (myself, hist, hmsg, hmsg);
	if (!strcmp ((char*) hmsg->msg->data, "ERROR")) {
		printf ("start_new_file reply: %s\n", (char*) hmsg->msg->data);
		return -1;
	}	
	
	IP_MsgDestroy (hmsg);

	return 0;
}

//----------------------------------------------------------------------------
//
//----------------------------------------------------------------------------
int trenddb_interpolate_data   (IP_Task *myself, IP_Task *hist,
								char *hist_name, double t_beg, double t_end,
								pt_t **values, int *nvalues)
{
	char			data [512];
	int				len;
	pt_t			*ptbuf;
	int				ptlen;
	int				i;
	int				step;
	int				count;
	int				offset;
	int				bufid = 0;
	int				ret;
	IP_Msg 			*smsg, *rmsg;

	smsg = IP_MsgCreate (NULL, IP_MsgDefaultSize(), 0);
	rmsg = IP_MsgCreate (NULL, IP_MsgDefaultSize(), 0);

	ret = 0;
	len = sprintf (data, "interpolate %f %f %s", t_beg, t_end, hist_name) + 1;
	IP_MsgCascade (smsg, data, len, IP_NONE, ST_OK);
	if (IP_TaskSendSync (myself, hist, smsg, rmsg) == 0) {
		if (!strcmp ((char*) rmsg->msg->data, "ERROR")) {
			printf ("interpolate reply: %s\n", (char*) rmsg->msg->data);
			ret = -1;
		} else {
			sscanf ((char*) rmsg->msg->data, "%d", &bufid);
		}
	} else {
		printf ("interpolate rmsg \"%s\", bufid = %d\n", (char*) rmsg->msg->data, bufid);
		perror ("");
		ret = -1;
	}
		
	ptlen = 0;
	len = sprintf (data, "buffer_id_lenght %d", bufid) + 1;
	IP_MsgCascade (smsg, data, len, IP_NONE, ST_OK);
	if (IP_TaskSendSync (myself, hist, smsg, rmsg) == 0) {
		if (!strcmp ((char*) smsg->msg->data, "ERROR")) {
			printf ("buffer_id_lenght reply: %s\n", (char*) rmsg->msg->data);
			ret = -1;
		} else {
			sscanf ((char*) rmsg->msg->data, "%d", &ptlen);
		}
	}
	
	ptbuf = malloc (ptlen * sizeof (pt_t));
	for (i= 0, step= 30; i< ptlen; i+= step) {
		count = min (step, (ptlen - i));
		offset = i;
		len = sprintf (data, "buffer_id_read %d %d %d", bufid, offset, count) + 1;
		IP_MsgCascade (smsg, data, len, IP_NONE, ST_OK);
		if (IP_TaskSendSync (myself, hist, smsg, rmsg) == 0) {
			if (strcmp ((char*) rmsg->msg->data, "ERROR")) {
				memcpy (&ptbuf [offset], rmsg->msg->data, rmsg->msg->nbytes);
			} else {
				printf ("buffer_id_read reply: %s\n", (char*) rmsg->msg->data);
				ret = -1;
			}
		}
	}

	fprintf (stderr, "%s, count = %d\n", hist_name, ptlen);
	
	len = sprintf (data, "buffer_id_destroy %d", bufid) + 1;
	IP_MsgCascade (smsg, data, len, IP_NONE, ST_OK);
	if (IP_TaskSendSync (myself, hist, smsg, rmsg) == 0) {
		if (!strcmp ((char*) rmsg->msg->data, "ERROR")) {
			printf ("bufid reply: %s\n", (char*) rmsg->msg->data);
			ret = -1;
		}
	}
	
	*values = ptbuf;
	*nvalues = ptlen;	
	
	IP_MsgDestroy (smsg);
	IP_MsgDestroy (rmsg);
	
	return ret;
}
